<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="module-array.html" />
<link rel="prev" href="module-heapq.html" />
<link rel="parent" href="datatypes.html" />
<link rel="next" href="bisect-example.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>5.5 bisect -- Array bisection algorithm</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.4.1 Theory"
  href="node92.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="5. data Types"
  href="datatypes.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="5.5.1 Examples"
  href="bisect-example.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node92.html">5.4.1 Theory</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="datatypes.html">5. Data Types</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="bisect-example.html">5.5.1 Examples</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h1><a name="SECTION007500000000000000000">
5.5 <tt class="module">bisect</tt> --
         Array bisection algorithm</a>
</h1>

<p>
<a name="module-bisect"></a>

<p>
This module provides support for maintaining a list in sorted order
without having to sort the list after each insertion.  For long lists
of items with expensive comparison operations, this can be an
improvement over the more common approach.  The module is called
<tt class="module">bisect</tt> because it uses a basic bisection algorithm to do its
work.  The source code may be most useful as a working example of the
algorithm (the boundary conditions are already right!).

<p>
The following functions are provided:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-754' xml:id='l2h-754' class="function">bisect_left</tt></b>(</nobr></td>
  <td><var>list, item</var><big>[</big><var>, lo</var><big>[</big><var>, hi</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  Locate the proper insertion point for <var>item</var> in <var>list</var> to
  maintain sorted order.  The parameters <var>lo</var> and <var>hi</var> may be
  used to specify a subset of the list which should be considered; by
  default the entire list is used.  If <var>item</var> is already present
  in <var>list</var>, the insertion point will be before (to the left of)
  any existing entries.  The return value is suitable for use as the
  first parameter to <code><var>list</var>.insert()</code>.  This assumes that
  <var>list</var> is already sorted.

<span class="versionnote">New in version 2.1.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-755' xml:id='l2h-755' class="function">bisect_right</tt></b>(</nobr></td>
  <td><var>list, item</var><big>[</big><var>, lo</var><big>[</big><var>, hi</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  Similar to <tt class="function">bisect_left()</tt>, but returns an insertion point
  which comes after (to the right of) any existing entries of
  <var>item</var> in <var>list</var>.

<span class="versionnote">New in version 2.1.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-756' xml:id='l2h-756' class="function">bisect</tt></b>(</nobr></td>
  <td><var>...</var>)</td></tr></table></dt>
<dd>
  Alias for <tt class="function">bisect_right()</tt>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-757' xml:id='l2h-757' class="function">insort_left</tt></b>(</nobr></td>
  <td><var>list, item</var><big>[</big><var>, lo</var><big>[</big><var>, hi</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  Insert <var>item</var> in <var>list</var> in sorted order.  This is equivalent
  to <code><var>list</var>.insert(bisect.bisect_left(<var>list</var>, <var>item</var>,
  <var>lo</var>, <var>hi</var>), <var>item</var>)</code>.  This assumes that <var>list</var> is
  already sorted.

<span class="versionnote">New in version 2.1.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-758' xml:id='l2h-758' class="function">insort_right</tt></b>(</nobr></td>
  <td><var>list, item</var><big>[</big><var>, lo</var><big>[</big><var>, hi</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
  Similar to <tt class="function">insort_left()</tt>, but inserting <var>item</var> in
  <var>list</var> after any existing entries of <var>item</var>.

<span class="versionnote">New in version 2.1.</span>

</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-759' xml:id='l2h-759' class="function">insort</tt></b>(</nobr></td>
  <td><var>...</var>)</td></tr></table></dt>
<dd>
  Alias for <tt class="function">insort_right()</tt>.
</dl>

<p>

<p><br /></p><hr class='online-navigation' />
<div class='online-navigation'>
<!--Table of Child-Links-->
<a name="CHILD_LINKS"><strong>Subsections</strong></a>

<ul class="ChildLinks">
<li><a href="bisect-example.html">5.5.1 Examples</a>
</ul>
<!--End of Table of Child-Links-->
</div>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="5.4.1 Theory"
  href="node92.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="5. data Types"
  href="datatypes.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="5.5.1 Examples"
  href="bisect-example.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="node92.html">5.4.1 Theory</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="datatypes.html">5. Data Types</a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="bisect-example.html">5.5.1 Examples</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
